package com.nulldev.util.web.nTube.pages.nmusic;

import java.util.Map;

import com.nulldev.util.VariableAPI.StringsUtil;
import com.nulldev.util.data.Arrays.maps.FastMaps;
import com.nulldev.util.manage.MarkedForRemoval;
import com.nulldev.util.web.nTubeExtensions.services.Deezer.Undeezerified;
import com.nulldev.util.web.nTubeExtensions.services.Deezer.Undeezerified.Song;

@MarkedForRemoval(reason = "nMusic is no more.")
public class DeezerSessionManager {

	/*
	 * TODO: - Add a hashing method - Provide a hash to use for security purposes. -
	 * Add a session removal call and interface
	 */

	private static final Map<String, Undeezerified> sessions = FastMaps.map();
	private static final Map<String, Song> songs = FastMaps.map();

	public static Undeezerified auto(final String arl) {
		if (arl == null || arl.isEmpty() || arl.length() <= 128)
			return null;
		if (sessions.containsKey(arl))
			return sessions.get(arl);
		final Undeezerified ud = new Undeezerified();
		try {
			if (ud.init(arl)) {
				sessions.put(arl, ud);
				return ud;
			} else {
				return null;
			}
		} catch (Exception e) {
			return null;
		}
	}

	public static Song trackGet(final String id) {
		if (songs.containsKey(id))
			return songs.remove(id);
		return null;
	}

	public static String registerTrack(final Song s) {
		final String id = StringsUtil.randomString(64);
		songs.put(id, s);
		return id;
	}
}
